package module.blog

class ArticleClassController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [articleClassInstanceList: ArticleClass.list(params), articleClassInstanceTotal: ArticleClass.count()]
    }

    def create = {
        def articleClassInstance = new ArticleClass()
        articleClassInstance.properties = params
        return [articleClassInstance: articleClassInstance]
    }

    def save = {
        ArticleClass.withTransaction{
            def articleClassInstance = new ArticleClass(params)
            if (articleClassInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.created.message', args: [message(code: 'articleClass.label', default: 'ArticleClass'), articleClassInstance.id])}"
                redirect(action: "show", id: articleClassInstance.id)
            }
            else {
                render(view: "create", model: [articleClassInstance: articleClassInstance])
            }
        }       
    }

    def show = {
        def articleClassInstance = ArticleClass.get(params.id)
        if (!articleClassInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'articleClass.label', default: 'ArticleClass'), params.id])}"
            redirect(action: "list")
        }
        else {
            [articleClassInstance: articleClassInstance]
        }
    }

    def edit = {
        def articleClassInstance = ArticleClass.get(params.id)
        if (!articleClassInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'articleClass.label', default: 'ArticleClass'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [articleClassInstance: articleClassInstance]
        }
    }

    def update = {
        ArticleClass.withTransaction{
            def articleClassInstance = ArticleClass.get(params.id)
            if (articleClassInstance) {
                if (params.version) {
                    def version = params.version.toLong()
                    if (articleClassInstance.version > version) {
                    
                        articleClassInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'articleClass.label', default: 'ArticleClass')] as Object[], "Another user has updated this ArticleClass while you were editing")
                        render(view: "edit", model: [articleClassInstance: articleClassInstance])
                        return
                    }
                }
                articleClassInstance.properties = params
                if (!articleClassInstance.hasErrors() && articleClassInstance.save(flush: true)) {
                    flash.message = "${message(code: 'default.updated.message', args: [message(code: 'articleClass.label', default: 'ArticleClass'), articleClassInstance.id])}"
                    redirect(action: "show", id: articleClassInstance.id)
                }
                else {
                    render(view: "edit", model: [articleClassInstance: articleClassInstance])
                }
            }
            else {
                flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'articleClass.label', default: 'ArticleClass'), params.id])}"
                redirect(action: "list")
            }
        }
    }
    
    def delete = {
        ArticleClass.withTransaction{
            def articleClassInstance = ArticleClass.get(params.id)
            if (articleClassInstance) {
                try {
                    articleClassInstance.delete(flush: true)
                    flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'articleClass.label', default: 'ArticleClass'), params.id])}"
                    redirect(action: "list")
                }
                catch (org.springframework.dao.DataIntegrityViolationException e) {
                    flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'articleClass.label', default: 'ArticleClass'), params.id])}"
                    redirect(action: "show", id: params.id)
                }
            }
            else {
                flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'articleClass.label', default: 'ArticleClass'), params.id])}"
                redirect(action: "list")
            }
        }
    }
}
